﻿// CWellsCfgDlg.cpp: 实现文件
//

#include "pch.h"
#include "RTOC-Data-Interface.h"
#include "CWellsCfgDlg.h"
#include "afxdialogex.h"


// CWellsCfgDlg 对话框

IMPLEMENT_DYNAMIC(CWellsCfgDlg, CDialogEx)

CWellsCfgDlg::CWellsCfgDlg(CWnd* pParent /*=nullptr*/)
	: CDialogEx(IDD_WELLSDLG, pParent)
{

}

CWellsCfgDlg::~CWellsCfgDlg()
{
}

void CWellsCfgDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialogEx::DoDataExchange(pDX);
	DDX_Control(pDX, IDC_LIST1, m_ctlList);
}


BEGIN_MESSAGE_MAP(CWellsCfgDlg, CDialogEx)
	ON_BN_CLICKED(IDOK, &CWellsCfgDlg::OnBnClickedOk)
	ON_NOTIFY(NM_CLICK, IDC_LIST1, &CWellsCfgDlg::OnNMClickList1)
	ON_BN_CLICKED(IDC_BTADD, &CWellsCfgDlg::OnBnClickedBtadd)
	ON_BN_CLICKED(IDC_BTDEL, &CWellsCfgDlg::OnBnClickedBtdel)
	ON_BN_CLICKED(IDC_ADDYQH, &CWellsCfgDlg::OnBnClickedAddyqh)
	ON_BN_CLICKED(IDC_ADDZXK, &CWellsCfgDlg::OnBnClickedAddzxk)
	ON_BN_CLICKED(IDC_ADDKD, &CWellsCfgDlg::OnBnClickedAddkd)
END_MESSAGE_MAP()


// CWellsCfgDlg 消息处理程序


BOOL CWellsCfgDlg::OnInitDialog()
{
	CDialogEx::OnInitDialog();

	// TODO:  在此添加额外的初始化
	m_ctlList.SetExtendedStyle(LVS_EX_FULLROWSELECT | LVS_EX_GRIDLINES);
	m_ctlList.InsertColumn(0, _T("序号"), LVCFMT_LEFT, 40);
	m_ctlList.InsertColumn(1, _T("井名"), LVCFMT_LEFT, 110);
	m_ctlList.InsertColumn(2, _T("井ID"), LVCFMT_LEFT, 130);
	m_ctlList.InsertColumn(3, _T("井筒ID"), LVCFMT_LEFT, 130);

	FillList();
	return TRUE;  // return TRUE unless you set the focus to a control
				  // 异常: OCX 属性页应返回 FALSE
}

void CWellsCfgDlg::FillList()
{
	m_ctlList.DeleteAllItems();
	CString strTemp;
	int nRows = gl.tlm.m_aWells.GetCount();
	for (int i = 0; i < nRows; i++)
	{
		SINGLE_WELL sw = gl.tlm.m_aWells.GetAt(i);
		strTemp.Format(_T("%d"), i+1);
		m_ctlList.InsertItem(i, strTemp, 0);
		m_ctlList.SetItemText(i, 1, sw.strWN);
		m_ctlList.SetItemText(i, 2, sw.strWellID);
		m_ctlList.SetItemText(i, 3, sw.strWBID);
	}
}

void CWellsCfgDlg::OnBnClickedOk()
{
	// TODO: 在此添加控件通知处理程序代码
	gl.m_bRunSync = FALSE;
	gl.SaveWellsCfg();
	if (m_pConMaster != NULL)
	{
		m_pConMaster->Close();
		m_pConMaster = NULL;
	}
	CDialogEx::OnOK();
}


void CWellsCfgDlg::OnNMClickList1(NMHDR *pNMHDR, LRESULT *pResult)
{
	LPNMITEMACTIVATE pNMItemActivate = reinterpret_cast<LPNMITEMACTIVATE>(pNMHDR);
	// TODO: 在此添加控件通知处理程序代码
	m_nCurSelRow = m_ctlList.GetSelectionMark();
	if (m_nCurSelRow >= 0)
	{
		m_strCurWN = m_ctlList.GetItemText(m_nCurSelRow, 1);
		m_strCurWN.Trim();

		GetDlgItem(IDC_WNTABLE)->SetWindowText(m_strCurWN + _T(" 手动建表"));
	}

	*pResult = 0;
}
//创建TSP_LTD_ACTIVE 表空间
//create tablespace TSP_LTD_ACTIVE  datafile 'E:\APP\EISS\ORADATA\KEEPDRILLING\TSP_LTD_ACTIVE.dbf' size 20m uniform size 128k
void CWellsCfgDlg::OnBnClickedBtadd()
{
	/*SINGLE_WELL sw;
	sw.strWN = _T("吐格6");
	sw.strWellID = _T("WELLTL110017499");
	sw.strWBID = _T("WEBHTL110021060");  //WEBHTL110021494
	gl.tlm.m_aWells.Add(sw);
	FillList();
	return;*/
	// TODO: 在此添加控件通知处理程序代码
	CString strWn, strBuf;
	GetDlgItem(IDC_WN)->GetWindowText(strWn);
	strWn.Trim();
	if (strWn != _T(""))
	{
		//gl.CreateLtdTable(strWn,FALSE);
		if(m_pConMaster == NULL)
			gl.OpenMasterDb(TRUE);
		if (m_pConSys == NULL)
			gl.OpenYqhDb(TRUE);
		if (m_pConKD == NULL)
			gl.OpenKdDb(TRUE);
		CString strWellID = gl.GetWellID(strWn);
		if (strWellID != _T(""))
		{
			CString strWellBoarID = gl.GetWellBoreID(strWellID);
			//strWn = _T("测试2022");
			if (strWellID != _T(""))
			{
				SINGLE_WELL sw;
				sw.strWN= strWn;
				sw.strWellID= strWellID;
				sw.strWBID= strWellBoarID;
				::EnterCriticalSection(&m_cWS);
				gl.tlm.m_aWells.Add(sw);
				::LeaveCriticalSection(&m_cWS);
				FillList();
				//创建新的时间表   0--一体化，1--中心库，2--KD库
				if (gl.m_nToYqh == 1) gl.CreateLtdTable(strWn, 0,FALSE);
				if(gl.m_nToMaster==1) gl.CreateLtdTable(strWn,1, FALSE);
				if (gl.m_nToKd == 1) gl.CreateLtdTable(strWn, 2, FALSE);
			}
			else
			{
				strBuf.Format(_T("井名为：%s 的井筒ID查询为空，不可添加！"), strWn);
				AfxMessageBox(strBuf, MB_ICONINFORMATION);
			}
		}
		else
		{
			strBuf.Format(_T("井名为：%s 的井ID查询为空，不可添加！"), strWn);
			AfxMessageBox(strBuf, MB_ICONINFORMATION);
		}
	}
}

void CWellsCfgDlg::OnBnClickedBtdel()
{
	// TODO: 在此添加控件通知处理程序代码
	if (m_nCurSelRow >= 0)
	{
		CString strTemp,strBuf;
		strTemp = m_ctlList.GetItemText(m_nCurSelRow, 1);
		strTemp.Trim();
		strBuf.Format(_T("请确认要删除井名为：%s ?"), strTemp);
		if (AfxMessageBox(strBuf, MB_YESNO | MB_ICONINFORMATION) == IDYES)
		{
			::EnterCriticalSection(&m_cWS);
			gl.tlm.m_aWells.RemoveAt(m_nCurSelRow);
			::LeaveCriticalSection(&m_cWS);
			FillList();
		}
	}
}


void CWellsCfgDlg::OnBnClickedAddyqh()
{
	// TODO: 在此添加控件通知处理程序代码
	if (m_strCurWN == _T(""))
	{
		AfxMessageBox(_T("请选择井，在井名所在行上单击选中！"), MB_ICONINFORMATION);
		return;
	}
	if (m_pConSys == NULL)
		gl.OpenYqhDb(TRUE);
	gl.CreateLtdTable(m_strCurWN, 0,TRUE);
}


void CWellsCfgDlg::OnBnClickedAddzxk()
{
	// TODO: 在此添加控件通知处理程序代码
	if (m_strCurWN == _T(""))
	{
		AfxMessageBox(_T("请选择井，在井名所在行上单击选中！"), MB_ICONINFORMATION);
		return;
	}
	if (m_pConMaster == NULL)
		gl.OpenMasterDb(TRUE);
	gl.CreateLtdTable(m_strCurWN, 1, TRUE);
}


void CWellsCfgDlg::OnBnClickedAddkd()
{
	// TODO: 在此添加控件通知处理程序代码
	if (m_strCurWN == _T(""))
	{
		AfxMessageBox(_T("请选择井，在井名所在行上单击选中！"), MB_ICONINFORMATION);
		return;
	}
	if (m_pConKD == NULL)
		gl.OpenKdDb(TRUE);
	gl.CreateLtdTable(m_strCurWN, 2, TRUE);
}
